package br.com.statizo.financeiro.view;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.wb.swt.SWTResourceManager;
import org.snoghertframework.swt.util.impl.LayoutUtil;

public class CalcListComposite extends Composite {


	private String titleText;
	private Color titleColor;
	private int titleHeight = 10;
	
	private int width = 98;
	private int height = 98;
	
	private int startLeftDefault = 0;
	private int startTopDefault = 2;
	private boolean pack = false;
	

	private LayoutUtil layoutUtil = new LayoutUtil();

	
	public CalcListComposite(Composite parent, int style, String title, Color titleColor) {
		super(parent, style);
		this.titleText = title;
		this.titleColor = titleColor;
		setLayout(new FormLayout());
		setBackground(new Color(Display.getDefault(),255,255,255));
		
		addPaintListener(new PaintListener(){

            @Override
            public void paintControl(PaintEvent e) {
            	if(pack)
            		pack();
            	Rectangle rect = getBounds();
                e.gc.setForeground(SWTResourceManager.getColor(204, 204, 204));
                e.gc.drawRoundRectangle(0, 0, rect.width-1, rect.height-1, 0, 0);
            }

        });

	}
	public CalcListComposite(Composite parent, int style) {
		this(parent,style,null,null);
	}
	
	
	public void setPack(boolean pack) {
		this.pack = pack;
	}
	
	protected void createTitle(){

		Composite composite = new Composite(this,SWT.NONE);
		composite.setLayout(new FormLayout());
		Label label = new Label(composite,SWT.BORDER_SOLID);
		label.setBackground(new Color(Display.getDefault(),238,238,238));
		label.setForeground(this.titleColor);
		Font font = new Font(Display.getCurrent(),"Verdana",12,SWT.BOLD);
		label.setFont(font);
		label.setText(titleText);
		label.setAlignment(SWT.CENTER);
		layoutUtil.setFormData(label, 0, 0, 100, 100);
		layoutUtil.setFormData(composite, 1, 1, 99, titleHeight);
		
	}
	
	
	protected boolean isTitlePresent(){
		return titleText!=null;
	}
	
	public void createContents() {

		boolean titlePresent = isTitlePresent();
		
		if(titlePresent)
			createTitle();
		
		Control[] children = getChildren();
		List<Control> calcControls = new ArrayList<>();
		
		for(int i = 0; i<children.length; i++){
			if(children[i] instanceof CalcItemComposite){
				calcControls.add(children[i]);
			}
			
		}
		
		int actualTop = titlePresent ? titleHeight + 1 : startTopDefault;
		int heightLen = titlePresent ? (height - titleHeight) / calcControls.size() : height/calcControls.size();
		
		for(int i = 0; i<calcControls.size(); i++){

			int actualBottom = actualTop + (heightLen)-1;
			layoutUtil.setFormData(calcControls.get(i), actualTop,startLeftDefault+1, width, actualBottom);
			actualTop = actualBottom + 1;
			
		}
		
	}
}